﻿using NJabber.Client.StanzaFactories;
using NJabber.Stream.Elements;
using System;
namespace NJabber.Client
{
    public class PresenceSubscriptionResponder : IPresenceSubscriptionResponder
    {
        private IXmppConnection connection;
        private ISubscriptionStanzaFactory factory;

        public PresenceSubscriptionResponder(IXmppConnection connection, ISubscriptionStanzaFactory factory) 
        {
            if (connection == null) throw new ArgumentNullException("connection");
            if (factory == null) throw new ArgumentNullException("factory");

            this.connection = connection;
            this.factory = factory;
        }

        public void SendApproveResponse(JID requestJid)
        {
            if (requestJid == null) throw new ArgumentNullException("requestJid");
            Stanza response = CreateApproveResponse(requestJid);
            SendStanza(response);
        }

        private Stanza CreateApproveResponse(JID jid)
        {
            return factory.CreateSubscriptionApproveResponse(jid);
        }

        public void SendDenyResponse(JID requestJid)
        {
            if (requestJid == null) throw new ArgumentNullException("requestJid");
            Stanza response = CreateDenyResponse(requestJid);
            SendStanza(response);
        }

        private Stanza CreateDenyResponse(JID jid)
        {
            return factory.CreateSubscriptionDenyResponse(jid);
        }

        private void SendStanza(Stanza stanza)
        {
            connection.Stream.Write(stanza);
        }
    }
}
